import { Query } from "../../../../../utils/Query";
import { Point } from "../../../../../utils/Point";
import { createDraw } from "../../../../../utils/createDraw";
export const TraEventManageHooks = () => {
  // 设置事件点的颜色样式 POINT_STYLE
  const changePointColor = (color) => {
    let colorId;
    switch (color) {
      case "绿色":
        colorId = 90;
        break;
      case "红色":
        colorId = 6;
        break;
      case "黄色":
        colorId = 4;
        break;
      default:
        break;
    }
    const POINT_STYLE = {
      Angle: 0,
      Color: colorId, //子图颜色,90绿色，6红色，4黄色
      Space: 0,
      SymHeight: 5,
      SymID: 21, //子图号
      SymWidth: 5,
    };
    return POINT_STYLE;
  };
  /* ===事件添加=== */
  // 添加点到地图 表单数据设置属性 画笔要素设置位置
  // 通过标记点添加点
  const addEventPoint = (formData, feature, docLayer) => {
    // 设置点要素的属性信息
    // 设置颜色样式
    let POINT_STYLE;
    switch (formData.status) {
      case 0:
        POINT_STYLE = changePointColor("红色");
        break;
      case 1:
        POINT_STYLE = changePointColor("黄色");
        break;
      case 2:
        POINT_STYLE = changePointColor("绿色");
        break;
      default:
        break;
    }
    // 设置点属性信息
    let FldName = [
      "事件编号",
      "事件类型",
      "事件等级",
      "发生时间",
      "发生地点",
      "车牌号",
      "驾驶员",
      "处理状态",
    ];
    let attr = [];
    let formValue = Object.values(formData);
    for (let i = 0; i < FldName.length; i++) {
      attr.push({
        name: FldName[i],
        type: typeof formValue[i],
        value: formValue[i],
      });
    }
    // 设置点位置信息
    let position = feature.getGeometry().getCoordinates();
    // 调用方法Point添加点
    let service = {
      name: "guanggu",
      layerId: 2,
    };
    Point.add({
      position,
      attr,
      service,
      docLayer,
      POINT_STYLE,
    });
  };
  // 通过坐标添加点
  const addEventPointByPosition = (formData, lnglat, docLayer) => {
    // 设置点要素的属性信息
    // 设置颜色样式
    let POINT_STYLE;
    switch (formData.status) {
      case 0:
        POINT_STYLE = changePointColor("红色");
        break;
      case 1:
        POINT_STYLE = changePointColor("黄色");
        break;
      case 2:
        POINT_STYLE = changePointColor("绿色");
        break;
      default:
        break;
    }
    // 设置点属性信息
    let FldName = [
      "事件编号",
      "事件类型",
      "事件等级",
      "发生时间",
      "发生地点",
      "车牌号",
      "驾驶员",
      "处理状态",
    ];
    let attr = [];
    let formValue = Object.values(formData);
    for (let i = 0; i < FldName.length; i++) {
      attr.push({
        name: FldName[i],
        type: typeof formValue[i],
        value: formValue[i],
      });
    }
    // 设置点位置信息
    let position = lnglat;
    // 调用方法Point添加点
    let service = {
      name: "guanggu",
      layerId: 2,
    };
    Point.add({
      position,
      attr,
      service,
      docLayer,
      POINT_STYLE,
    });
  };

  /* ===事件更新=== */
  // 更新点的样式 即处理状态
  // 设置事件点三种样式 设置高亮图层
  const trafficEventStyle = (status) => {
    // status 事件处理状态
    let srcTE;
    switch (status) {
      case "0":
        srcTE = "/src/assets/image/trafficEventRed.png";
        break;
      case "1":
        srcTE = "/src/assets/image/trafficEventYellow.png";
        break;
      case "2":
        srcTE = "/src/assets/image/trafficEventGreen.png";
        break;
      default:
        break;
    }
    const styleTrafEvt = new ol.style.Style({
      /**{olx.style.IconOptions}类型*/
      image: new ol.style.Icon({
        anchor: [0.5, 200],
        anchorOrigin: "top-right",
        anchorXUnits: "fraction",
        anchorYUnits: "pixels",
        offsetOrigin: "top-right",
        // offset:[0,20],
        //图标缩放比例
        scale: 0.15,
        //透明度
        opacity: 0.75,
        //图标的url
        src: srcTE,
      }),
    });
    return styleTrafEvt;
  };
  // 更新点
  const updateEventPoint = (formData, feature, docLayer) => {
    // 设置点要素的属性信息
    // 设置颜色样式
    let POINT_STYLE;
    switch (formData.status) {
      case 0:
        POINT_STYLE = changePointColor("红色");
        break;
      case 1:
        POINT_STYLE = changePointColor("黄色");
        break;
      case 2:
        POINT_STYLE = changePointColor("绿色");
        break;
      default:
        break;
    }
    // 设置点属性信息
    let FldName = [
      "事件编号",
      "事件类型",
      "事件等级",
      "发生时间",
      "发生地点",
      "车牌号",
      "驾驶员",
      "处理状态",
    ];
    let attr = [];
    let formValue = Object.values(formData);
    for (let i = 0; i < FldName.length; i++) {
      attr.push({
        name: FldName[i],
        type: typeof formValue[i],
        value: formValue[i],
      });
    }
    // 设置点位置信息
    let position = feature.getGeometry().getFlatCoordinates();
    // 获取点的FID
    let FID = feature.getId();
    // 调用方法Point修改点
    let service = {
      name: "guanggu",
      layerId: 2,
    };
    Point.update({
      FID,
      position,
      attr,
      service,
      docLayer,
      POINT_STYLE,
    });
  };

  return {
    addEventPoint,
    addEventPointByPosition,
    trafficEventStyle,
    updateEventPoint,
  };
};
